
package com.insigma.business.qrcode.soldier._bff.impl;

import com.alibaba.fastjson.JSONObject;
import com.insigma.business.qrcode.common.dto.CommonAdminAreaDTO;
import com.insigma.business.qrcode.common._consumer.CommonAdminAreaConsumer;
import com.insigma.business.qrcode.common._consumer.SystemConfigConsumer;
import com.insigma.business.qrcode.common.util.*;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.soldier.dto.ContactNoDTO;
import com.insigma.business.qrcode.soldier.dto.SimpleApiRequest;
import com.insigma.business.qrcode.soldier.dto.SoldierApplyInfoDTO;
import com.insigma.business.qrcode.soldier.dto.SoldierBasicInfoDTO;
import com.insigma.business.qrcode.soldier.dto.gov.BaseProviderImpl;
import com.insigma.business.qrcode.soldier.request.AddressComponent;
import com.insigma.business.qrcode.soldier.request.AddressResponse;
import com.insigma.business.qrcode.soldier.request.BaseApiRequest;
import com.insigma.business.qrcode.soldier._bff.CommonAreaBffProvider;
import com.insigma.business.qrcode.soldier._consumer.SoldierApplyInfoConsumer;
import com.insigma.business.qrcode.soldier._consumer.SoldierBasicInfoConsumer;
import com.insigma.business.qrcode.soldier.util.OkHttp3Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class CommonAreaBffProviderImpl extends BaseProviderImpl implements CommonAreaBffProvider {

    @Autowired
    private CommonAdminAreaConsumer commonAdminAreaConsumer;

    @Autowired
    private SoldierBasicInfoConsumer soldierBasicInfoConsumer;

    @Autowired
    private SoldierApplyInfoConsumer soldierApplyInfoConsumer;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private SystemConfigConsumer systemConfigConsumer;

    @Override
    public BaseResponse<List<CommonAdminAreaDTO>> findSubAreaList(BaseApiRequest request) {
        return commonAdminAreaConsumer.findSubAreaList(request.getAdminCode());
    }

    @Override
    public BaseResponse<List<CommonAdminAreaDTO>> findQGSubAreaList(BaseApiRequest request) {
        return commonAdminAreaConsumer.findQGSubAreaList(request.getAdminCode());
    }

    @Override
    public BaseResponse<CommonAdminAreaDTO> getArea(BaseApiRequest request) {
        CommonAdminAreaDTO adminAreaById = commonAdminAreaConsumer.getAdminAreaById(request.getAdminAreaCode());
        return ResponseUtils.buildSuccessResponse(adminAreaById);
    }

    @Override
    public BaseResponse<List<CommonAdminAreaDTO>> test(BaseApiRequest adminAreaCode) {
        commonAdminAreaConsumer.testCleanSetSoldier();
        return null;
    }

    @Override
    public BaseResponse<CommonAdminAreaDTO> getParent(SimpleApiRequest request) {

        String adminAreaCode = request.getAdminAreaCode();
        BizCheckUtil.checkNotNull(adminAreaCode, "地区码不能为空");
        CommonAdminAreaDTO adminAreaOfEmployee = commonAdminAreaConsumer.findAdminAreaOfEmployee();
        String fatherCode = adminAreaOfEmployee.getId();
        if (AdminAreaCodeUtils.getCutdAreaCode(fatherCode).length() < AdminAreaCodeUtils.getCutdAreaCode(adminAreaCode).length()) {
            String filledFatherCode = AdminAreaCodeUtils.getFilledFatherCode(adminAreaCode);
            CommonAdminAreaDTO adminAreaById = commonAdminAreaConsumer.getAdminAreaById(filledFatherCode);

            return ResponseUtils.buildSuccessResponse(adminAreaById);

        } else {
            return ResponseUtils.buildSuccessResponse(null);
        }
    }

    @Override
    public BaseResponse<String> getAreaCodeByAddress(String address) {
        BaseResponse<String> stringBaseResponse = commonAdminAreaConsumer.analyseAreaCodeByBasicInfoForOther(address);
        return stringBaseResponse;
    }

    @Override
    public String getCachedAdminCodeOfSoldier(String soldierId) {

        return getCachedSoldierAdminCode(soldierId);
    }

    @Override
    public String getCachedAdminCodeOfIdCardNo(String idCardNo) {
        return getCachedIdCardNoAdminCode(idCardNo);
    }

    @Override
    public BaseResponse<ContactNoDTO> queryCommonContactNo(SimpleApiRequest request) {

        if (request == null || request.getAdminAreaCode() == null) {
            SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
            if (sessionSoldier != null) {
                BaseResponse<SoldierApplyInfoDTO> applyInfo = soldierApplyInfoConsumer.getSoldierApplyInfoBySoldierId(sessionSoldier.getUserId());
                if (applyInfo.getDto() != null) {
                    request.setAdminAreaCode(applyInfo.getDto().getResidentAdminAreaCode());
                }
            }

        }
        return systemConfigConsumer.queryCommonContactNo(request);
    }

    private String getCachedIdCardNoAdminCode(String idCardNo) {
        String key = "ADMIN_CODE_OF_IDCARDNO_" + idCardNo;
        Object o = redisUtils.get(key);
        if (o == null) {
            SoldierBasicInfoDTO dto = soldierBasicInfoConsumer.getSoldierBasicInfoByCard(idCardNo).getDto();
            BizCheckUtil.checkNotNull(dto);
            o = dto.getResidentAdminAreaCode();
            redisUtils.set(key, o, 60 * 60 * 24);
        }
        String str = (String)o;
        CommonAdminAreaDTO adminAreaById = commonAdminAreaConsumer.getAdminAreaById(str);
        return adminAreaById.getId();
    }

    private String getCachedSoldierAdminCode(String soldierId) {
        String key = "ADMIN_CODE_OF_SOLDIER_" + soldierId;
        Object o = redisUtils.get(key);
        if (o == null) {
            SoldierBasicInfoDTO dto = soldierBasicInfoConsumer.getSoldierBasicInfoBySoldierId(soldierId).getDto();
            if (dto == null) {
                o = "330100000000";
            } else {
                o = dto.getResidentAdminAreaCode();
            }
            redisUtils.set(key, o, 60 * 60 * 24);
        }
        String str = (String)o;
        CommonAdminAreaDTO adminAreaById = commonAdminAreaConsumer.getAdminAreaById(str);
        return adminAreaById.getId();
    }

    public static void main(String[] args) {


        String lng = "120.081919";
        String lat = "30.284187";


        String str = OkHttp3Util.sendByGetUrl("http://api.map.baidu.com/reverse_geocoding/v3/?ak=pXlEit6Ca8HHeVgeS647nmpjFFfAmriF&output=json&coordtype=wgs84ll&location=" + lat + "," + lng );

        AddressResponse addressResponse = JSONObject.parseObject(str, AddressResponse.class);
        AddressComponent addressComponent = addressResponse.getResult().getAddressComponent();
        System.out.println();
    }
}
